package com.eonmind.ai.core.video.platform.pixverse.api;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.eonmind.ai.core.video.api.IAiVideoService;
import com.eonmind.ai.core.video.platform.enums.AiVideoPlatformEnum;
import com.eonmind.ai.core.video.platform.pixverse.model.PixverseTaskQueryRes;
import com.eonmind.ai.core.video.platform.pixverse.model.PixverseTaskSubmitReq;
import com.eonmind.common.contract.constants.HttpHeaders;
import com.eonmind.common.contract.constants.ai.AIVideoPlatformApiConstant;
import com.eonmind.common.contract.exception.BusinessException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public abstract class AbstractPixVerseService implements IAiVideoService<PixverseTaskSubmitReq,PixverseTaskQueryRes> {
    protected PixverseTaskQueryRes jsonToResult(JSONObject bodyJson) {
        ObjectMapper mapper = new ObjectMapper();
        PixverseTaskQueryRes pixverseTaskQueryRes = null;
        bodyJson = bodyJson.getJSONObject("data");
        try {
            pixverseTaskQueryRes = mapper.readValue(JSONUtil.toJsonStr(bodyJson), PixverseTaskQueryRes.class);
        } catch (JsonProcessingException e) {
            log.error("Pixverse 响应结果转换异常：{}",e);
        }
        return pixverseTaskQueryRes;
    }

    protected String buildTaskSubmitBody(PixverseTaskSubmitReq aiVideoTask) {
        Map<String,Object> body = new HashMap<String,Object>();
        body.put(AIVideoPlatformApiConstant.Pixverse.Params.SubmitTaskParams.Prompt,aiVideoTask.getPrompt());
        body.put(AIVideoPlatformApiConstant.Pixverse.Params.SubmitTaskParams.Radio,aiVideoTask.getRatio());
        if(ObjectUtils.isNotEmpty(aiVideoTask.getStyle())) {
            body.put(AIVideoPlatformApiConstant.Pixverse.Params.SubmitTaskParams.Style,aiVideoTask.getStyle());
        }
        if(ObjectUtils.isNotEmpty(aiVideoTask.getSeed())) {
            body.put(AIVideoPlatformApiConstant.Pixverse.Params.SubmitTaskParams.Seed,aiVideoTask.getSeed());
        }
        if(ObjectUtils.isNotEmpty(aiVideoTask.getNegativePrompt()))  {
            body.put(AIVideoPlatformApiConstant.Pixverse.Params.SubmitTaskParams.NegativePrompt,aiVideoTask.getNegativePrompt());
        }
        body.put(AIVideoPlatformApiConstant.Pixverse.Params.SubmitTaskParams.Model,aiVideoTask.getModel());
        if(ObjectUtils.isNotEmpty(aiVideoTask.getTemplateId())) {
            body.put(AIVideoPlatformApiConstant.Pixverse.Params.SubmitTaskParams.TemplateId,aiVideoTask.getTemplateId());
        }
        return JSONUtil.toJsonStr(body);
    }

    protected Map<String, String> buildHeader(AiVideoPlatformEnum aiVideoPlatform) {
        if(ObjectUtils.anyNull(aiVideoPlatform,aiVideoPlatform.getApiKey(),aiVideoPlatform.getBaseUrl())) {
            throw new BusinessException("Pixverse 平台参数错误");
        }
        Map<String,String> headers = new HashMap<>();
        headers.put(HttpHeaders.AUTHORIZATION,"Bearer "+aiVideoPlatform.getApiKey());
        return headers;
    }

    protected Map<String, String> buildTaskQueryParams(String taskId) {
        Map<String,String> params = new HashMap<>();
        params.put(AIVideoPlatformApiConstant.Pixverse.Params.QueryTaskParams.TaskId,taskId);
        return params;
    }
}
